﻿component
{
	include "../../inc/lib.cfm";
	//列表操作
	public any function list(any rc)
	{
		var result = {};
		//从对象中进行Hql模式查询
		var offset = (rc.page-1)*rc.rows;
		hql = "from role where " & rc.sqlWhere & rc.sqlSort;
		var result["rows"] = ormExecuteQuery(hql, false, {maxResults=rc.rows, offset=offset});
		var totalhql = "select count(id) as total from role where " & rc.sqlWhere ;
		var result["total"] = ormExecuteQuery(totalhql, true);

		return result;
	}

	public any function getTree(){
		var result = {};
		var hql = "from role";
		var result = ormExecuteQuery(hql,false);

		for(i = 1; i <= arraylen(result); i++)
		{
			result[i]['text'] = result[i].getName();
		}

		return result;
	}


	//删除操作
	public any function delete(numeric id){
		transaction {
            var role = entityLoadByPk("role", arguments.id);
           	entityDelete(role);
            transactionCommit();
        }
	}

	//获取操作
	public any function get(numeric id){
		result = entityLoadByPk("role", arguments.id);
		return result;
	}

	//新增操作
	public any function save(any rc){
		transaction {
            var q = entityNew("role");
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}

	//修改操作
	public any function update(any rc){
		transaction {
            var q = entityLoadByPk("role", rc.id);
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}

	public any function getRoleResource(numeric id){
		var result = {};
		var hql = "from roleresource where roleid = " & id;
		var result = ormExecuteQuery(hql,false);
		return result;
	}

	//授权操作
	public any function savegrant(any rc){
		transaction {
			//在授权之前，先要检查ids是否已经存在在roleresource中
			/*
			ids需要分成三部分
			1.需要新添加的
			2.不需要操作的
			3.要删除操作的

			示例如下：
			ids1 = "1,2,3,4,5"; //新传递进来的值
			ids2 = "1,3,8,9";	//原来已经存在的值
			writeoutput(":" & listCompare(ids1,ids2));
			writeoutput("不需要操作的:" & ListInCommon(ids1,ids2));
			writeoutput("要删除操作的:" & listdelete(ListInCommon(ids1,ids2),ids2));
			需要新添加的:2,4,5
			不需要操作的:1,3
			要删除操作的:8,9
			*/
			var orObj = getRoleResource(rc.id);
			oldIds = "";
			checkIds = "";

			for(var i=1;i<=arraylen(orObj);i++){
				oldIds = listAppend(oldIds,orObj[i].getResourceid().getId());
				checkIds = listAppend(checkIds,orObj[i].getId());
			}
			needInsertIds = listCompare(rc.ids,oldIds);
			keepStatusIds = ListInCommon(rc.ids,oldIds);
			needDeleteIds = listdelete(ListInCommon(rc.ids,oldIds),oldIds);

			//writeoutput("1:" & needInsertIds & "--2:" & keepStatusIds & "--3:" & needDeleteIds);
			//需要删除的部分
            if(listlen(needDeleteIds)){
				for(var i=1;i<=listlen(needDeleteIds);i++){
					var deleteId =  listgetat(checkIds, listfindnocase( oldIds,listgetat(needDeleteIds,i) )) ;
					var orEntity = entityLoadByPk("roleresource", deleteId);
					entityDelete(orEntity);
				}
			}

			//需要添加的部分
			if(listlen(needInsertIds)){
				for(var i=1;i<=listlen(needInsertIds);i++){
					var q = entityNew("roleresource");
					//建立resource实体，进行赋值
					var r = entityNew("resource");
					r.setid(listgetat(needInsertIds,i));
					//建立role实体，进行赋值
					var o = entityNew("role");
					o.setId(rc.id);
					//将对象存储至roleresource实体中去
					q.setResourceid(r);
					q.setRoleid(o);
					entitySave(q);
				}
            }

            transactionCommit();
        }
        return true;
	}
}